#!/usr/bin/env python
import sys
import pygtk, gtk
import cairo
import FoldTree

class FoldingMargin():
	
	def __init__( self, TextEditor ):
		self.TextEditor   = TextEditor	
		self.FoldTree     = FoldTree.FoldTree( self )
		self.LineNrMargin = self.TextEditor.LineNrMargin

	def Draw( self, context, widget, expose_event, width, height ):
		self.TextEditor.SetCairoColorFromRgb( context, 255, 255, 255 )
		line_nr_margin    = self.LineNrMargin.MarginArea
		context.rectangle(line_nr_margin, 0, line_nr_margin, height)
		context.fill()
		#deseneaza nodeurile
		for segment in self.FoldTree.List:
			y, height = self.TextEditor.SourceView.get_line_yrange( segment.StartLineIter )
			y         = y + 4
			segment.X = line_nr_margin + 2
			segment.Y = -self.TextEditor.GetVScrollBarValue() + y
			if segment.Collapsed:
				self.DrawNode( expose_event.window, segment.X, segment.Y, True )
			else:
				self.DrawNode( expose_event.window, segment.X, segment.Y, False )
				
	def IsOnNode( self, mouse_x, mouse_y ):
		ret = None
		for segment in self.FoldTree.List:
			if segment.IsOnNode( mouse_x, mouse_y ):
				ret = segment
				break
		return ret
		
	def DrawNode( self, drawable,  x, y, collapsed ):
		pixbuf = gtk.gdk.pixbuf_new_from_file("Images/c.png")
		if collapsed: 
			pixbuf = gtk.gdk.pixbuf_new_from_file("Images/c2.png")
		#
		drawable.draw_pixbuf( self.TextEditor.get_window().new_gc(), pixbuf, 0, 0, int(x), int(y) )		
	
	
